<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">

      <el-form-item label="商品名称" prop="commodityName">
        <el-input v-model="dataForm.commodityName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="commodityNum">
        <el-input v-model="dataForm.commodityNum" placeholder="商品数量"><template slot="append">件</template></el-input>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select v-model="dataForm.customerId" placeholder="请选择">
          <el-option
            v-for="item in optionsCustomer"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="租仓" prop="storageId">
        <el-cascader
          v-model="defaultCascaderValue"
          :options="storageOptions"
          :props="myProp"
          @change="handleChange"></el-cascader>
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input-number v-model="dataForm.area" placeholder="面积"></el-input-number>
      </el-form-item>
      <el-form-item label="租赁开始日期" prop="leaseStartTime">
        <el-date-picker v-model="dataForm.leaseStartTime" placeholder="租赁开始日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="租赁结束日期" prop="leaseEndTime">
        <el-date-picker v-model="dataForm.leaseEndTime" placeholder="租赁结束日期"></el-date-picker>
      </el-form-item>


      <el-form-item label="商品种类" prop="commodityType">
        <el-select filterable v-model="dataForm.commodityType" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value*1">
          </el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        defaultCascaderValue: [], // 默认值
        myProp: {
          value: 'value',
          label: 'label',
          checkStrictly: true
        },
        options: [],
        optionsCustomer: [],
        storageOptions: [],
        visible: false,
        dataForm: {
          id: 0,
          commodityBatch: '',
          commodityName: '',
          storageId: '',
          storageName: '',
          spaceId: '',
          spaceName: '',
          commodityNum: '',
          area: '',
          leaseStartTime: '',
          leaseEndTime: ''
        },
        dataRule: {
        }
      }
    },
    methods: {
      handleChange (value) {
        if (value[0] != null) {
          this.dataForm.storageId = value[0]
        }
        if (value[1] != null) {
          this.dataForm.spaceId = value[1]
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entinventory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.entId = data.entInventory.entId
                this.dataForm.commodityBatch = data.entInventory.commodityBatch
                this.dataForm.commodityName = data.entInventory.commodityName
                this.dataForm.storageId = data.entInventory.storageId
                this.dataForm.storageName = data.entInventory.storageName
                this.dataForm.spaceId = data.entInventory.spaceId
                this.dataForm.spaceName = data.entInventory.spaceName
                this.dataForm.commodityNum = data.entInventory.commodityNum
                this.dataForm.area = data.entInventory.area
                this.dataForm.leaseStartTime = data.entInventory.leaseStartTime
                this.dataForm.leaseEndTime = data.entInventory.leaseEndTime
                this.defaultCascaderValue.push(data.entInventory.storageId, data.entInventory.spaceId)
                this.dataForm.customerId = data.entInventory.customerId
                this.dataForm.commodityType = data.entInventory.commodityType
              }
            })
          }
        })

      // 客户接口
        this.$http({
          url: this.$http.adornUrl(`/app/entcustomer/listCustomer`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.optionsCustomer = data.list
          } else {
            this.optionsCustomer = []
          }
        })
      // 商品种类
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/commodity_type`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
      // 级联仓库库位
        this.$http({
          url: this.$http.adornUrl(`/app/entcoldstorage/cascader`),
          method: 'post',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.storageOptions = data.entColdStorage
          } else {
            this.storageOptions = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entinventory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'commodityBatch': this.dataForm.commodityBatch,
                'commodityName': this.dataForm.commodityName,
                'storageId': this.dataForm.storageId,
                'customerId': this.dataForm.customerId,
                'storageName': this.dataForm.storageName,
                'spaceId': this.dataForm.spaceId,
                'spaceName': this.dataForm.spaceName,
                'commodityNum': this.dataForm.commodityNum,
                'commodityType': this.dataForm.commodityType,
                'area': this.dataForm.area,
                'leaseStartTime': this.dataForm.leaseStartTime,
                'leaseEndTime': this.dataForm.leaseEndTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
